package com.sea.supply.service.impl;


import com.codecrane.core.web.BatchImportUtils;
import com.sea.physical.entity.CreatePlan;
import com.sea.supply.entity.*;
import com.sea.supply.mapper.ProduceDetailMapper;
import com.sea.supply.mapper.WareProduceMapper;
import com.sea.supply.service.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.List;


@Service
public class WareProduceServiceImpl implements WareProduceService {

    @Autowired
    private WareProduceMapper wareProduceMapper;
    @Autowired
    private ReceiveService receiveService;
    @Autowired
    private ProduceDetailService produceDetailService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private WareDistributeService wareDistributeService;
    @Autowired
    private ProduceService produceService;
    @Autowired
    private ProduceDetailMapper produceDetailMapper;



    @Override
    public int save(WareProduce wareProduce) {
        return wareProduceMapper.insert(wareProduce);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importSku(InputStream inputStream, String fileName, String userRealName) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream, fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<WareProduce> list = BatchImportUtils.iteratorForReceiveWare(sheet);
            if (null == list || list.size() == 0) {
                throw new Exception("请至少保存一条SKU收货信息！");
            } else {

                Integer produceId=null;
                for (WareProduce receive: list) {
                    String produceNo = receive.getProduceNo();
                    String receiveChannelName = receive.getReceiveChannelName();
                    String receiveSku = receive.getReceiveSku();
                    Integer receiveNumber = receive.getReceiveNumber();
                    String orderDate = receive.getOrderDate();
                    String wareName = receive.getWareName();

                    Supplier supplier = supplierService.findBySupplierName(wareName);
                    if(null==supplier || supplier.getSupplierWare().equals("否")){
                        throw new RuntimeException("暂无查询到国内仓信息！【仓库:" + wareName + ";】");
                    }
                    WareProduce receiveProduce = wareProduceMapper.findReceiveNumber(receive);
                    if(null==receiveProduce){
                        throw new RuntimeException("暂无查询到数据！【SKU:" + receiveSku + ";渠道:" + receiveChannelName + ";PO:" + produceNo + ";预计交期:" + orderDate + "】");
                    }

                    receiveProduce.getId();
                    //获取可收货余量

                    Integer produceNumber = receiveProduce.getReceiveNumber();

                    if(produceNumber-receiveNumber<0){
                        throw new RuntimeException("【SKU:" + receiveSku + ";渠道:" + receiveChannelName + ";PO:" + produceNo + ";预计交期:" + orderDate + "】可收货数量不能小于0");
                    }

                    if(produceNumber-receiveNumber>receiveProduce.getProduceTotal()){
                        throw new RuntimeException("【SKU:" + receiveSku + ";渠道:" + receiveChannelName + ";PO:" + produceNo + ";预计交期:" + orderDate + "】数量不能大于采购数量");
                    }

                    receive.setProduceSupplierName(receiveProduce.getProduceSupplierName());
                    receive.setDetailId(receiveProduce.getId());
                    receive.setProduceUse(0);
                    receive.setReceiveMember(userRealName);
                    wareProduceMapper.insert(receive);
                    //更新主表
                    wareDistributeService.save(receive);
                    //更新剩余量
                    produceDetailService.updateSurplusNumber(receiveNumber, receiveProduce.getId());
                    //更新表头信息
                    produceId=produceDetailService.findById(receiveProduce.getId()).getProduceId();
                    produceService.updateProduceTotal(produceId);

                    //更新收货表
                    Receive rec=new Receive();
                    rec.setProduceDetailId(receiveProduce.getId());
                    rec.setProduceSupplierName(wareName);
                    rec.setReceiveNo(produceNo);
                    rec.setReceiveChannelName(receiveChannelName);
                    rec.setReceiveSku(receiveSku);
                    rec.setReceiveNumber(receiveNumber);
                    rec.setReceiveDate(receive.getReceiveDate());
                    rec.setReceiveMember(userRealName);
                    receiveService.save(rec);


                    //修改采购单状态
                    Produce pro = produceService.findById(produceId);
                    //查询可收获数量
                    Integer res = pro.getProduceSurplus();
                    if (res > 0 || res<0) {
                        //部分收货
                        pro.setProduceStatus(31);
                        pro.setFlag(null);
                        produceService.usave(pro);
                    }
                    if (res == 0) {
                        //全部收货
                        pro.setProduceStatus(32);
                        pro.setFlag(null);
                        produceService.usave(pro);
                    }
                }
            }
        }
    }

    @Override
    public WareProduce findReceiveNumber(WareProduce wareProduce) {
        return wareProduceMapper.findReceiveNumber(wareProduce);
    }

    @Override
    public List<WareProduce> findByCnd(WareProduce wareProduce) {
        return wareProduceMapper.findByCnd(wareProduce);
    }

    @Override
    public List<WareProduce> getDistributeNumber(String receiveSku, String receiveChannelName) {
        return wareProduceMapper.getDistributeNumber(receiveSku, receiveChannelName);
    }

    @Override
    public int updateUseNumber(Integer useNumber, Integer id) {
        return wareProduceMapper.updateUseNumber(useNumber, id);
    }

    @Override
    public WareProduce findById(Integer id) {
        return wareProduceMapper.findById(id);
    }

    @Override
    public Integer getDistributeNumberBySkuChannel(String receiveSku, String receiveChannelName) {
        return wareProduceMapper.getDistributeNumberBySkuChannel(receiveSku, receiveChannelName);
    }

    @Override
    public WareProduce findWareProduce(WareProduce wareProduce) {
        return wareProduceMapper.findWareProduce(wareProduce);
    }
}
